package com.zy.crm.workbench.controller;




import com.zy.crm.workbench.domain.GuoJi;
import com.zy.crm.workbench.domain.HangDao;
import com.zy.crm.vo.Message;
import com.zy.crm.vo.PaginationVo;
import com.zy.crm.workbench.service.HangDaoService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CYH
 * 2021年11月13日测试功能完成
 */
//
@Controller
public class HangDaoController {

    @Resource
    private HangDaoService HangDaoService;


    // 添加用户信息，无误
    @RequestMapping("/workbench/hangDao/saveHangDao.do")
    @ResponseBody
    public Object saveActivity(HangDao hangdao){
        //测试
        System.out.println("进入保存成功，前端无误，继续排查");
        System.out.println(hangdao);
        // 得到保存结果
        Message msg = HangDaoService.saveHangDao(hangdao);

        return msg;
    }

    @RequestMapping("/workbench/hangDao/pageList.do")
    @ResponseBody
    // 查询用分页
    public Object getPageList(String pageNo, String pageSize,HangDao hangdao){

       // System.out.println("测试数据传输了什么"+hangdao);
        PaginationVo<HangDao> paginationVo = HangDaoService.getPageList(pageNo,pageSize,hangdao);
        //System.out.println(paginationVo);
        return paginationVo;
    }

    // 删除根据id,无误
    @RequestMapping("/workbench/hangDao/delete.do")
    @ResponseBody
    public Message deleteActivity(HttpServletRequest request) {

        // 拿到ActivityId数组
        String HangDaoIds[] = request.getParameterValues("id");
        System.out.println("第一条数据是："+HangDaoIds);
        // 根据id数组删除市场活动cce5c8636660495d839bd0f661cc5d66
                           //cce5c8636660495d839bd0f661cc5d665
        Message msg = HangDaoService.deleteHangDao(HangDaoIds);
        System.out.println("删除成功");
        return msg;
    }


    // 修改市用户数据依据id,测试中
    @RequestMapping("/workbench/hangDao/updateHangDao.do")
    @ResponseBody
    public Message updateActivityById(HangDao hangdao){
       // System.out.println("成功添加数据进入了修改数据");
        System.out.println("测试传入的数据是否异常"+hangdao);
        //测试到传入的name和id都是null，估计是前端问题
        Message msg = HangDaoService.updateHangDaoById(hangdao);
        System.out.println("成功出来");
        return msg;

    }

    //根据用户id查询一条用户信息
    @RequestMapping("/workbench/hangDao/getHangDaoById.do")
    @ResponseBody
    public Map<String , Object> getHangDaoById(String id){
        Map<String , Object> map = new HashMap<>();
        System.out.println("测试获取原来的信息"+id);
        // 查询到市场活动信息
        HangDao hangdao = HangDaoService.getHangDaoById(id);
        //测试是否有结果返回,结果很明显是进入业务层出错，继续测试
        System.out.println("测试是否有结果返回");
        // 封装入map
        System.out.println(hangdao);
        map.put("hangdaoById",hangdao);//测试结果是user里面有name的，是前端有问题

        return  map;
    }
    @RequestMapping("/workbench/hangDao/pageList1.do")//报表打印
    @ResponseBody
    // 打印数据
    public Message getPageList1(HangDao center) throws IOException {

        List<HangDao> list = HangDaoService.getPrint(center);
        //System.out.println("获取的数据是"+list);
        Message msg = new Message();

        String path = "D:\\ssm项目\\crm-master -day11\\";
        Workbook workbook = new HSSFWorkbook();//工作簿
        Sheet sheet = workbook.createSheet("航道费纪录表");//工作表
        //（1，1）
        Row row1 = sheet.createRow(0);//第一行数据
        Cell cell11 = row1.createCell(0);//第一列数据
        cell11.setCellValue("序号");//内容
        //(1,2)
        Cell cell12 = row1.createCell(1);//第二列数据
        cell12.setCellValue("船名");//内容

        Cell cell13 = row1.createCell(2);//第三列数据
        cell13.setCellValue("航道费用");//内容
        Cell cell14 = row1.createCell(3);//第四列数据
        cell14.setCellValue("交付日期");//内容
        Cell cell15 = row1.createCell(4);//第五列数据
        cell15.setCellValue("费用有效期");//内容

        String arr[][] = new String[list.size()][5];

        for (int i = 0; i < list.size(); ++i) {
            arr[i][0] = list.get(i).getId();
            arr[i][1] = list.get(i).getName();
            arr[i][2] = list.get(i).getCost();
            arr[i][3] = list.get(i).getWrite_time();
            arr[i][4] = list.get(i).getMonth_status();


        }


        for (int i = 1; i <= list.size(); ++i) {
            Row row = sheet.createRow(i);//第I+1行数据
            for (int j = 0; j <5; ++j) {
                Cell cell = row.createCell(j);
                cell.setCellValue(arr[i - 1][j]);//
            }

        }

        //生成一个表
        FileOutputStream fileOutputStream= new FileOutputStream(path+"航道费纪录表.xls");

        workbook.write(fileOutputStream);
        //关闭流
        workbook.close();

        System.out.println("成功生成");


        msg.setSuccess(true);

        return  msg;

    }
}


















